package com.tsx.demo;

//PdfAnnotation 对象
//该PdfAnnotation类的包com.itextpdf.kernel.pdf.annot代表所有注释的超类。
//在其派生类中，PdfLinkAnnotation类表示链接注释。创建这个类的一个对象，如下。
//设置注解的动作
//使用PdfLinkAnnotation类的setAction()方法将操作设置为注释，如下所示。

import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Link;
import com.itextpdf.layout.element.Paragraph;

import java.nio.file.Paths;

public class LinkAnnotation {
    public static void main(String[] args) throws Exception {
        // Creating a PdfWriter
        String dest = Paths.get(Constants.dest, "linkAnnotation.pdf").toString();

        PdfWriter writer = new
                PdfWriter(dest);

        // Creating a PdfDocument
        PdfDocument pdf = new PdfDocument(writer);

        // Creating a Document
        Document document = new Document(pdf);

        // Creating a PdfLinkAnnotation object
        Rectangle rect = new Rectangle(0, 0);
        PdfLinkAnnotation annotation = new PdfLinkAnnotation(rect);

        // Setting action of the annotation
        PdfAction action = PdfAction.createURI("https://www.baidu.com/");
        annotation.setAction(action);

        // Creating a link
        Link link = new Link("Click here", annotation);

        // Creating a paragraph
        Paragraph paragraph = new Paragraph("Hi welcome to Baidu! ");

        // Adding link to paragraph
        paragraph.add(link.setUnderline());

        // Adding paragraph to document
        document.add(paragraph);

        // Closing the document
        document.close();
        pdf.close();
        writer.close();

        System.out.println("Annotation added successfully");
    }
}  